package cn.cvs.chap4bootmybatis.controller;

import cn.cvs.chap4bootmybatis.entity.Book;
import cn.cvs.chap4bootmybatis.service.BookService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class BookController {
    @Autowired
    BookService bookService;
    @GetMapping("/book")
    public String getAll(Model model){
        List<Book> list=bookService.getAll();
        model.addAttribute("list",list);
        return "index";
    }


    @GetMapping("/page")
    public String getPage(@RequestParam(defaultValue = "1") Integer pageNum,Model model){
        PageInfo page=bookService.getPage(pageNum);
        model.addAttribute("page",page);
        return "page";
    }

    @PostMapping("/del")
    public String del(Integer[] ids){
        if (ids.length>0){
            bookService.del(ids);
        }
        return "redirect:/page";
    }
}
